#include"Vector.h"

Vector::Vector(const Vector& temp):sz{temp.sz},elem{new double[temp.sz]},space{temp.space}
{
  copy(temp.elem,temp.elem+temp.sz,elem);
}

Vector& Vector::operator=(const Vector& temp)
{
  if(&temp==this) return *this;
  double* p=new double[temp.sz];
  copy(temp.elem,temp.elem+temp.sz,p);
  space=sz=temp.sz;
  delete []elem;
  elem=p;
  return *this;
}

double& Vector::operator[](int n)
{
  return elem[n];
}

double Vector::operator[](int n) const
{
  return elem[n];
}

void Vector::print()
{
  for(int i=0;i<sz;i++)
    cout<<elem[i]<<' ';
  cout<<endl;
}

void Vector::reserve(int newalloc)
{
  if(newalloc<=space)
    return;
  double* p=new double[newalloc];
  copy(elem,elem+sz,p);
  delete[] elem;
  space=newalloc;
  elem=p;
}

void Vector::resize(int newsize)
{
  reserve(newsize);
  for(int i=sz;i<newsize;i++)
    elem[i]=0;
  sz=newsize;
}

void Vector::push_back(double d)
{
  if(space==sz)
    reserve(space*2);
  else if(space==0)
    reserve(8);
  elem[sz]=d;
  sz++;
}
